<?php
$addnew = false; //判断是否为新增
$enable = 1;
if (isset($this->tables['codeinfo'])) {
    $row = $this->tables['codeinfo'];
    $enable = $row['enable'];
} else {
    $addnew = true; //如果没数据那就是新增
}
?>
<script>
    function switchClassName(obj,name1,name2){
        $(obj).find("input").each(function(){
            var classnamearr=this.className.split(' ');
            var i=$.inArray(name1, classnamearr);
            if(i!=-1){
                $(this).removeClass(name1).addClass(name2);
            }
        });
    }
    function validateCallback1(obj, obj2){
        var selobj=$("select[name='typename']");
        var typename=selobj.val();
        var div=$("div[typename="+typename+"]");
        var json={};
        div.find(":input").each(function(){
            json[this.name]=this.value.trim();
        });
        var proctext=(JSON.stringify(json));
        $("input[name='proctext']").val(proctext);
        return validateCallback(obj, obj2);
    }
    function typeSelectChange(obj){
        var curtype=$(obj).val();
        $(obj).find("option").each(function(){
            var v=$(this).val();
            if(v!=""){
                var div=$("div[typename="+v+"]");
                if(div.length==0){
                    alert("不存在["+v+"]！");
                }else if(v==curtype){
                    switchClassName(div,"unrequired","required");
                    //div.show();
                    div.css("position","");
                    //div.css("visibility","visible");
                }else{
                    switchClassName(div,"required","unrequired");
                    //div.hide();
                    div.css("position","absolute");
                    div.css("top","-1000px");
                    div.css("left","-1000px");
                    //div.css("visibility","hidden");
                }
            }
        });
    }
    $(function(){
        var selobj=$("select[name='typename']");
        selobj.change(function(){
            typeSelectChange(this);
        });
        var typename="<?= isset($row['typename']) ? $row['typename'] : '' ?>";
        var proctext="<?= isset($row['proctext']) ? addslashes($row['proctext']) : '' ?>";
        var proctextjson={};
        try{
            proctextjson=JSON.parse(proctext);
        }catch(e){}
        $("input[name='proctext']").val(proctext);
        selobj.val(typename);//设置SELECT的选择值
        typeSelectChange(selobj);//自动初始化输入框
        var div=$("div[typename="+typename+"]");
        for(var key in proctextjson){
            var val=proctextjson[key];
            //$(div).find("input[name='"+key+"']").val(val);
            $(div).find(":input[name='"+key+"']").val(val);
        }
        //*/
    });
</script>
<div class="pageContent">
    <form method="post" action="<?= $this->action ?>" class="pageForm required-validate" onsubmit="return validateCallback1(this, dialogAjaxDone);">
        <div class="pageFormContent" layoutH="56">
            <?php if (isset($row['id'])) { ?>
                <dl style="display: none;">
                    <dt>代码编号：</dt>
                    <dd><input name="id" type="text" size="30" value="<?= $row['id'] ?>" readonly="readonly"/></dd>
                </dl>
            <?php } else { ?>
                <input name="viewnameid" type="hidden" size="30" value="<?= $_GET['viewnameid'] ?>" readonly="readonly"/>
                <dl style="display: none;">
                    <dt>菜单编号：</dt>
                    <dd><input name="menuitemid" type="text" size="30" value="<?= $_GET['menuitemid'] ?>" readonly="readonly"/></dd>
                </dl>
                <dl style="display: none;">
                    <dt>父级ID号：</dt>
                    <dd><input name="parentid" type="text" size="30" value="<?= $_GET['parentid'] ?>" readonly="readonly"/></dd>
                </dl>
                <dl style="display: none;">
                    <dt>设置nextid：</dt>
                    <dd><input name="nextid" type="text" size="30" value="<?= isset($_GET['nextid']) ? intval($_GET['nextid']) : 0 ?>" readonly="readonly"/></dd>
                </dl>
            <?php } ?>
            <dl>
                <dt>代码标题：</dt>
                <dd><input name="title" class="required" type="text" size="30" value="<?= isset($row['title']) ? $row['title'] : '' ?>" alt="请简要描述代码的主要功能"/></dd>
            </dl>
            <dl>
                <dt>是否生效：</dt>
                <dd><input type="radio" name="enable" value="0"<? if ($enable == 0) { ?> checked<? } ?> /><font color="#999999">失效</font>
                    <input type="radio" name="enable" value="1"<? if ($enable == 1) { ?> checked<? } ?> /><font color="green">生效</font>
                </dd>
            </dl>
            <div class="divider"></div>
            <dl>
                <dt>代码类型：</dt>
                <dd>
                    <select name="typename" class="required">
                        <option value="">===请选择===</option>
                        <option value="newclass">NEW初始化类</option>
                        <option value="callclass">CALL调用类的方法</option>
                        <option value="classvar">给类的属性赋值</option>
                        <option value="if">IF开始判断{...}</option>
                        <option value="elseif">IF否则再判断{...}</option>
                        <option value="else">IF结束判断{...}</option>
                        <option value="for">FOR循环{...}</option>
                    </select>
                </dd>
            </dl>
            <dl style="display:none;">
                <dt>proctext</dt>
                <dd><input type="text" name="proctext" value=""/></dd>
            </dl>
            <div typename="newclass">
                <dl>
                    <dt>选择要NEW的类：</dt>
                    <dd><input name="class" type="text" size="30" class="unrequired" alt="例如：model_procedure" value="" /></dd>
                </dl>
                <dl>
                    <dt>填写构造函数的参数：</dt>
                    <dd><input name="parameter" type="text" size="30" alt="例如：" value="" /></dd>
                </dl>
                <dl>
                    <dt>赋值到变量：</dt>
                    <dd><input name="assignvarname" type="text" size="30" alt="例如：$procedure" value="" /></dd>
                </dl>
            </div>
            <div typename="callclass">
                <dl>
                    <dt>取赋有Class的变量：</dt>
                    <dd><input name="classvar" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <dl>
                    <dt>选择您要调用方法：</dt>
                    <dd><input name="function" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <dl !class="nowrap" style="height:50px;">
                    <dt>填写方法的参数：</dt>
                    <dd><textarea name="parameter" cols="30" rows="3"></textarea></dd>
                </dl>
                <dl>
                    <dt>将返回值赋到变量：</dt>
                    <dd><input name="assignvarname" type="text" size="30" /></dd>
                </dl>
            </div>
            <div typename="classvar">
                <dl>
                    <dt>取赋有Class的变量：</dt>
                    <dd><input name="classvar" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <dl>
                    <dt>你要设置的属性：</dt>
                    <dd><input name="varname" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <dl style="height:50px;">
                    <dt>你要设置的值：</dt>
                    <dd><textarea name="object" cols="30" rows="3" class="unrequired" ></textarea>
                </dl>
            </div>
            <div typename="if">
                <dl>
                    <dt>要判断的条件：</dt>
                    <dd><input name="condition" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <?php if (0 && !$addnew) { ?>
                    <dl>
                        <dt>当条件满足则执行：</dt>
                        <dd><a rel="sys_procedure_list_<?= $row['id'] ?>" target="navTab" href="?/sys_procedure_list&menuitemid=<?= $_GET['menuitemid'] ?>&parentid=<?= $row['id'] ?>" title="代码列表(if)">查看代码列表</a></dd>
                    </dl>
                <?php } ?>
            </div>
            <div typename="elseif">
                <dl>
                    <dt>要判断的条件：</dt>
                    <dd><input name="condition" type="text" size="30" class="unrequired" /></dd>
                </dl>
                <?php if (0 && !$addnew) { ?>
                    <dl>
                        <dt>当条件满足则执行：</dt>
                        <dd><input name="subid" type="text" size="10" /></dd>
                    </dl>
                <?php } ?>
            </div>
            <div typename="else">
                <?php if (0 && !$addnew) { ?>
                    <dl>
                        <dt>要执行的子语句：</dt>
                        <dd><input name="subid" type="text" size="10" /></dd>
                    </dl>
                <?php } ?>
            </div>
            <div typename="for">

            </div>
        </div>
        <div class="formBar">
            <ul>
                    <!--<li><a class="buttonActive" href="javascript:;"><span>保存</span></a></li>-->
                <li><div class="buttonActive"><div class="buttonContent"><button type="submit">保存</button></div></div></li>
                <li>
                    <div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div>
                </li>
            </ul>
        </div>
    </form>
</div>
